package people.paper.tv.activity.home.modual.sptt.view.impl;

import android.content.Context;
import android.support.annotation.NonNull;
import android.util.Log;
import android.view.Gravity;
import android.view.KeyEvent;
import android.view.View;
import android.widget.FrameLayout;

import com.skyworth.ui.api.SkyWithBGLoadingView;
import com.skyworth.util.Util;

import people.paper.tv.R;

public class VideoLayout extends FrameLayout implements View.OnKeyListener, View.OnFocusChangeListener,
        View.OnClickListener {
    View mFocusView, videoPlayer;
    boolean isBigVideo = false;
    SkyWithBGLoadingView loadingView;
    VideoSeekBar seekBar;
    FrameLayout content;

    public VideoLayout(@NonNull Context context) {
        super(context);
        setFocusable(true);
        setFocusableInTouchMode(true);
        setOnFocusChangeListener(this);
        setOnKeyListener(this);
        setOnClickListener(this);
        setBackgroundResource(R.color.video_play_bg);

        mFocusView = new View(context);
        mFocusView.setBackgroundResource(R.mipmap.block_focus);
        int focusW = Util.Div(30+1049);
        int focusH = Util.Div(98+590);
        LayoutParams focusParams = new LayoutParams(focusW, focusH);
        focusParams.topMargin = Util.Div(-20);
        focusParams.leftMargin = Util.Div(-16);
        addView(mFocusView,focusParams);
        mFocusView.setVisibility(GONE);

        seekBar = new VideoSeekBar(context);
        LayoutParams seekParam = new LayoutParams(Util.Div(1920), Util.Div(100), Gravity.BOTTOM);
        seekParam.bottomMargin = Util.Div(50);
        addView(seekBar, seekParam);
        seekBar.setVisibility(GONE);
        seekBar.setParentVideo(this);

        content = new FrameLayout(getContext());
        addView(content, new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
    }

    public void setOrientation(boolean isHorizontal){
        refreshVideo();
    }

    public void refreshVideo(){
        if(isBigVideo){
            if(videoPlayer != null) {
                LayoutParams params = (LayoutParams) videoPlayer.getLayoutParams();
                params.width = headlineLayout.fullW;
                params.height = headlineLayout.fullH;
                params.gravity = Gravity.CENTER;
                videoPlayer.setLayoutParams(params);
            }
        }else {
            if(videoPlayer != null){
                LayoutParams params = (LayoutParams) videoPlayer.getLayoutParams();
                params.width = headlineLayout.width;
                params.height = headlineLayout.height;
                params.gravity = Gravity.CENTER;
                videoPlayer.setLayoutParams(params);
            }
        }
        LayoutParams videoParams = (LayoutParams) mFocusView.getLayoutParams();
        videoParams.width = Util.Div(30) + headlineLayout.width;
        videoParams.height = Util.Div(98) + headlineLayout.height;
        mFocusView.setLayoutParams(videoParams);
    }

    public void addVideoPlayer(View video) {
        if(videoPlayer == null){
            videoPlayer = video;
            int w, h;
            if(isBigVideo){
                w = headlineLayout.fullW;
                h = headlineLayout.fullH;
            }else {
                w = headlineLayout.width;
                h = headlineLayout.height;
            }
            LayoutParams params = new LayoutParams(w, h, Gravity.CENTER);
//            LayoutParams params = new LayoutParams(Util.Div(1049), Util.Div(590));
            content.addView(videoPlayer, params);
        }
    }

    public void clearVideoPlayer() {
        if (videoPlayer != null)
            content.removeView(videoPlayer);
        videoPlayer = null;
    }

    @Override
    public void onFocusChange(View v, boolean hasFocus) {
        mFocusView.setVisibility(hasFocus?VISIBLE:GONE);
    }

    @Override
    public void onClick(View v) {
        if(videoPlayer != null && !isBigVideo){
            headlineLayout.refreshFullVideo(true);
            refreshFullVideo(true);
        }
    }

    @Override
    public boolean onKey(View v, int keyCode, KeyEvent event) {
        Log.d("VideoSeekBar", "--- onKey isBigVideo ---"+isBigVideo+"; "+keyCode);
        if(event.getAction() == KeyEvent.ACTION_DOWN){
            if(isBigVideo){
                if(keyCode == KeyEvent.KEYCODE_BACK){
                    headlineLayout.refreshFullVideo(false);
                    refreshFullVideo(false);
                }
                return true;
            }else if(keyCode == KeyEvent.KEYCODE_DPAD_LEFT){
                return headlineLayout.setListFocus();
            }else if(keyCode == KeyEvent.KEYCODE_DPAD_DOWN || keyCode == KeyEvent.KEYCODE_DPAD_RIGHT){
                return true;
            }
        }
        return false;
    }

   HeadlineLayout headlineLayout;
   public void setParentView(HeadlineLayout l){
       headlineLayout = l;
   }

//   public boolean mOnKey(View v, int keyCode, KeyEvent event){
//       return headlineLayout.onKey(v, keyCode, event);
//   }

    public void refreshFullVideo(boolean isFull){
        isBigVideo = isFull;
        if(isFull){
            seekBar.getFocus();
//            LayoutParams contentParams = (LayoutParams) content.getLayoutParams();
//            contentParams.width = Util.Div(1920);
//            contentParams.height = Util.Div(1080);
//            contentParams.leftMargin = Util.Div(-754);
//            contentParams.topMargin = Util.Div(-305);
//            content.setLayoutParams(contentParams);
            content.setBackgroundResource(R.color.video_play_bg);

            if(videoPlayer != null){
                LayoutParams params = (LayoutParams) videoPlayer.getLayoutParams();
                params.width = headlineLayout.fullW;
                params.height = headlineLayout.fullH;
                params.gravity = Gravity.CENTER;
                videoPlayer.setLayoutParams(params);
            }
        }else {
            this.requestFocus();
            seekBar.setVisibility(GONE);

//            LayoutParams contentParams = (LayoutParams) content.getLayoutParams();
//            contentParams.width = LayoutParams.MATCH_PARENT;
//            contentParams.height = LayoutParams.MATCH_PARENT;
//            contentParams.topMargin = 0;
//            contentParams.leftMargin = 0;
//            content.setLayoutParams(contentParams);
            content.setBackground(null);

            if(videoPlayer != null){
                LayoutParams params = (LayoutParams) videoPlayer.getLayoutParams();
                params.width = headlineLayout.width;
                params.height = headlineLayout.height;
                params.gravity = Gravity.CENTER;
                videoPlayer.setLayoutParams(params);
                Log.i("onNewsLoad", "SPTTViewImpl exitFullScreen 1:"+params.width+"; "+videoPlayer);
            }
        }
    }
/*
    public void refreshFullVideoPause(){
        isBigVideo = false;
        seekBar.setVisibility(GONE);
        content.setBackground(null);

        if(videoPlayer != null){
            LayoutParams params = (LayoutParams) videoPlayer.getLayoutParams();
            params.width = headlineLayout.width;
            params.height = headlineLayout.height;
            params.gravity = Gravity.CENTER;
            videoPlayer.setLayoutParams(params);
            Log.i("onNewsLoad", "SPTTViewImpl exitFullScreen 1:"+params.width+"; "+videoPlayer);
        }
    }*/

    public void setListVisible(boolean listVisible){
        if(listVisible){
            headlineLayout.setListVisible(listVisible);
            seekBar.setVisibility(GONE);
        }else {
            seekBar.getFocus();
        }
    }

    public void showLoading() {
        if (loadingView == null) {
            loadingView = new SkyWithBGLoadingView(getContext());
            LayoutParams loadParam = new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, Gravity.CENTER);
            addView(loadingView, loadParam);
        }
        loadingView.setVisibility(View.VISIBLE);
        loadingView.showLoading();
        loadingView.bringToFront();
        if (loadingView.getParent() != null)
            loadingView.getParent().requestLayout();
    }

    public void hideLoading() {
        if (loadingView != null) {
            loadingView.dismissLoading();
        }
    }

    public void exitFullScreen(){
        headlineLayout.exitFullScreen();
    }

    ISPTTViewCallBack callBack;
    public void setParentViewCallback(ISPTTViewCallBack c) {
        callBack = c;
        seekBar.setParentViewCallback(c);
    }

    public void refreshDuration(long total, long duration){
        seekBar.refreshDuration(total, duration);
    }
}
